package com.ma.lucenedemo.util;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class TabsExcelUtil {

    public static Workbook isExcelFile(InputStream inputStream) {
        Workbook workbook = null;
        String Type = "";
        try {
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            if (POIFSFileSystem.hasPOIFSHeader(bis)) {
                workbook = new HSSFWorkbook(bis);
                Type = "XLS";
            } else if (POIXMLDocument.hasOOXMLHeader(bis)) {
                workbook = new XSSFWorkbook(OPCPackage.open(bis));
                Type = "XLSX";
            }
        } catch (FileNotFoundException e) {
            System.exit(0);
        } catch (IOException e) {

        }catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e) {
            e.printStackTrace();
        }

        return workbook;
    }
}
